<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2016/10/26
 * Time: 19:57
 */
function dbInit(){
    $link = mysqli_connect('localhost','root','','itcast');
    if(!$link) {
        die('链接数据库失败  ' . mysqli_error($link));
    }
    mysqli_query($link,'set names utf8');
}
function query($link,$sql){
    if($result = mysqli_query($link,$sql)) {
        return $result;
    }else{
        echo 'SQL执行失败:<br>';
        echo '错误的SQL为:',$sql,'<br>';
        echo '错误的代码为:',mysqli_error($sql),'<br>';
        echo '错误的信息为:',mysqli_errno($sql),'<br>';
        die;
    }
}
function fetchAll($link,$sql){
    if($result = query($link,$sql)){
        $rows = array();
        while( $row = mysqli_fetch_array($result,MYSQLI_ASSOC)){
            $rows = $row;
        }
        mysqli_free_result($result);
        return $rows;
    }else{
        return false;
    }
}
function fetchRow($link,$sql)
{
    if ($result = query($link,$sql)) {
        $row = mysqli_fetch_array($result, MYSQLI_ASSOC);
        return $row;
    } else {
        return false;
    }
}
function safeHandle($link,$data)
{
    $data = htmlspecialchars($data);
    $data = mysqli_real_escape_string($link,$data);
    return $data;
}
